package com.gin.kotlin.no_gay_alliance.adapter.ban

import android.content.Context
import android.content.DialogInterface
import android.view.View
import com.gin.kotlin.no_gay_alliance.R
import com.gin.kotlin.no_gay_alliance.activity.UserCenterActivity
import com.gin.kotlin.no_gay_alliance.app.App
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingRecyclerViewAdapter
import com.gin.kotlin.no_gay_alliance.base.AlertDialogUtils
import com.gin.kotlin.no_gay_alliance.base.MyHandler
import com.gin.kotlin.no_gay_alliance.base.ViewUtils.Companion.visible
import com.gin.kotlin.no_gay_alliance.database.entity.BanUser
import com.gin.kotlin.no_gay_alliance.databinding.ItemBanUserBinding

class BanUserAdapter(context: Context, handler: MyHandler) : AbstractBindingRecyclerViewAdapter<ItemBanUserBinding, BanUser>(context, handler,false), View.OnClickListener {
    override fun onBindViewHolder(holder: ViewHolder<ItemBanUserBinding>, position: Int, b: ItemBanUserBinding, dataModel: BanUser) {
        b.tvUsername.text = dataModel.username ?: "UID${dataModel.userId}"
        b.tvRemark.text = dataModel.remark
        b.tvRemark.visibility = visible(dataModel.remark)


        listOf(b.tvUsername, b.tvRemark, b.ivDelete).forEach {
            it.tag = dataModel
            it.setOnClickListener(this)
        }

    }

    override fun onClick(v: View?) {
        val banUser = v?.tag as? BanUser ?: return
        val index = data.indexOf(banUser)

        println("index = ${index}")
        when (v.id) {
            R.id.iv_delete -> AlertDialogUtils.builder(context, "移除用户: ${banUser.username}", listener = { _, which ->
                if (which == DialogInterface.BUTTON_POSITIVE) {
                    App.INSTANCE.database.banUserDao().del(banUser.userId)
                    removeData(index)
                }
            }).show()

            else -> UserCenterActivity.startActivity(context, banUser.userId)
        }


    }
}